#!/bin/python3
from hw4_LED import *
import sys

if __name__ == "__main__":
    # 直接调用
    # l4 = LED(4)
    # l4 = LED(4)
    # 会有bug
    try:
        print("Usage: ")
        print("  open <LED>")
        print("  close <LED>")
        print("  state <LED>")
        print("  Put Ctrl+C or EOF (Ctrl+D) to Stop")
        print("")
        print("Example: ")
        print("  open 4 -- open LED4")
        print("  close 4 -- close LED4")
        print("  state 4 -- display state of LED4")
        print("")
        while True:
            line = input("> ")
            words = line.split()
            words = [word for word in words if word]
            if len(words) != 2: 
                print("miss")
                continue
            try:
                if words[0] == "open":
                    LEDFactory.getLED(int(words[1])).turnON()
                elif words[0] == "close":
                    LEDFactory.getLED(int(words[1])).turnOFF()
                elif words[0] == "state":
                    LEDFactory.getLED(int(words[1])).dispState()
            except ValueError as ve:
                sys.stderr.write(str(ve) + '\n')
    except (KeyboardInterrupt, EOFError):
        print("")